package com.bawei.yangzongcai___chenmei.LoginStart;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.bawei.yangzongcai___chenmei.MaiuActivity.MainActivity;
import com.bawei.yangzongcai___chenmei.R;

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private EditText username;
    private EditText password;
    private CheckBox cbRemember;
    private CheckBox cbAutoLogin;
    private Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        cbRemember = findViewById(R.id.cb_remember);
        cbAutoLogin = findViewById(R.id.cb_auto_login);
        login = findViewById(R.id.login);

        //TODO 读取
        sharedPreferences=getSharedPreferences("1609A", Context.MODE_PRIVATE);
        boolean ischeck= sharedPreferences.getBoolean("ischeck",false);
        boolean isauto= sharedPreferences.getBoolean("isauto",false);
        if(ischeck){
            //读到用户名和密码展现在页面中,复选框被勾选
            String username1=sharedPreferences.getString("username","");
            String password1=sharedPreferences.getString("password","");
            username.setText(username1);
            password.setText(password1);
            cbRemember.setChecked(true);
        }
        if(isauto){//自动登录被选中
            Intent intent= new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
        }

        //TODO 写数据
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username2=username.getText().toString().trim();
                String password2=password.getText().toString().trim();
                //用户名和密码是否正确
                if("yang".equals(username2)&&"123456".equals(password2)){
                    //判断记住密码是否被选中
                    if(cbRemember.isChecked()){//存储数据
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("ischeck",true);
                        edit.putString("username",username2);
                        edit.putString("password",password2);
                        edit.commit();
                    }else{//更新ischecked为false
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("ischeck",false);
                        edit.commit();
                    }
                    //判断自动登录是否被选中
                    if(cbAutoLogin.isChecked()){//存储数据
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("isauto",true);
                        edit.commit();
                    }else{//更新isauto为false
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("isauto",false);
                        edit.commit();
                    }
                    Intent intent= new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });




    }
}
